package com.hotau.seller;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hotau.HotauActivity;
import com.hotau.R;
import com.hotau.framework.RightBtnVisible;
import com.hotau.seller.vo.SellerDetails;
import com.hotau.ui.Gallery;
import com.hotau.ui.PageIndicatorView;

public class SellerDetailFragment extends Fragment implements
		OnItemSelectedListener, OnItemClickListener,
		RightBtnVisible {

	private Gallery snapshotGallery;
	private RatingBar seller_detail_rate;

	private ImageView seller_detail_promote_image;
	private ImageView seller_detail_icon;

	private TextView seller_detail_distance;
	private TextView seller_detail_title;

	private ImageAdapter mAdapter;

	public static int galleryImageHeight = 90;
	public static int galleryImageWidth = 100;

	private PageIndicatorView mPageView;

	private static final int[] mPicture = new int[] { R.drawable.banner01,
			R.drawable.banner02, R.drawable.banner03, R.drawable.banner04,
			R.drawable.banner05, R.drawable.banner06 };

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.seller_item_detail, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		snapshotGallery = (Gallery) getActivity().findViewById(R.id.snapshotGallery);
		seller_detail_rate = (RatingBar) getActivity().findViewById(R.id.seller_detail_rate);
		seller_detail_promote_image = (ImageView) getActivity().findViewById(R.id.seller_detail_promote_image);
		seller_detail_icon = (ImageView) getActivity().findViewById(R.id.seller_detail_icon);
		seller_detail_distance = (TextView) getActivity().findViewById(R.id.seller_detail_distance);
		seller_detail_title = (TextView) getActivity().findViewById(R.id.seller_detail_title);
		mPageView = (PageIndicatorView) getActivity().findViewById(R.id.gallery_indicator_iview);
		
		initData();
	}

	private void initData() {
		SellerDetails detail = new SellerDetails("宏天五金店", null, 4, 100, "五金杂货",
				"宏天五金杂货店专售家居生活中需要的床柜，灯具，热水器，空调，冰箱等电器所用到的零部件", "满20元包送上门",
				"支付宝，财付通，货到付款");
		seller_detail_title.setText(detail.getSellerName());
		seller_detail_distance.setText(detail.getDistance());
		seller_detail_icon.setImageResource(R.drawable.banner01);
		seller_detail_rate.setRating(detail.getSellerStarCount());

		mAdapter = new ImageAdapter(getActivity());
		snapshotGallery.setAdapter(mAdapter);
		if (mPicture != null && mPicture.length > 1) {
			snapshotGallery.setSelection(1);
		}

		mAdapter.notifyDataSetChanged();
		snapshotGallery.setAdapter(mAdapter);
	}

	class ImageAdapter extends BaseAdapter {
		Context context;

		public ImageAdapter(Context ctx) {
			this.context = ctx;
		}

		@Override
		public int getCount() {
			return mPicture.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressWarnings("deprecation")
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(context);
			LinearLayout layout = new LinearLayout(context);
			int width = (int) (galleryImageWidth * HotauActivity.galleryImageRate);
			int height = (int) (galleryImageHeight * HotauActivity.galleryImageRate);
			layout.setLayoutParams(new Gallery.LayoutParams(width, height));
			layout.addView(imageView);
			imageView.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setBackgroundResource(mPicture[position]);
			return layout;
		}
	}

	@Override
	public void processRightBtn(Context context, Button rightBtn) {
		rightBtn.setText("宝贝");
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		mPageView.setCurrentPage(position);
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		mPageView.setCurrentPage(position);
	}
	
	@Override
	public void onNothingSelected(AdapterView<?> parent) {

	}

}
